package main

import (
	"flag"
	"fmt"
)

func FunctionA() {
	ok := flag.Bool("ok", false, "is ok")
	id := flag.Int("id", 0, "id")
	port := flag.String("port", ":8080", "http listen port")
	var name string
	flag.StringVar(&name, "name", "123", "name")

	flag.Parse()

	fmt.Println("ok:", *ok)
	fmt.Println("id:", *id)
	fmt.Println("port:", *port)
	fmt.Println("name:", name)
}

func FunctionB() {
	data_path := flag.String("D", "/home/manu/sample/", "DB data path")
	log_file := flag.String("l", "/home/manu/sample.log", "log file")
	nowait_flag := flag.Bool("W", false, "do not wait until operation completes")

	//flag.Parse解析 "-key=alue 或者 -option的类型" 的命令行参数,一旦遇到 "-key=alue 或者 -option的类型" 的参数解析就会停止
	flag.Parse()

	//获取flag.Parse解析剩下之后的参数
	var cmd string = flag.Arg(0)

	fmt.Printf("action   : %s\n", cmd)
	fmt.Printf("data path: %s\n", *data_path)
	fmt.Printf("log file : %s\n", *log_file)
	fmt.Printf("nowait   : %v\n", *nowait_flag)

	fmt.Printf("-------------------------------------------------------\n")

	//遍历no-flag的参数(no-flag参数:是flag.Parse解析停止之后剩下的参数)
	fmt.Printf("there are %d non-flag input param\n", flag.NArg())
	for i, param := range flag.Args() {
		fmt.Printf("#%d    :%s\n", i, param)
	}
}

func main() {
	// -D /home/manu/DB_data/ -l /home/manu/DB_data/postgres_manu.log -W start
	// -l=/home/manu/DB_data/postgres_manu.log -W -D /home/manu/DB_data/  start
	// start -l=/home/manu/DB_data/postgres_manu.log -W -D /home/manu/DB_data/
	//FunctionB()

	FunctionA()
}
